package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftBattleDetailData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
public class PeakSeacraftBattleDetailDataDaoImpl extends TempDataObjectDao<PeakSeacraftBattleDetailData> {
    public static String getPeakSeacraftBattleDetailDataSql = "select * from `t_c_activity_peak_seacraft_battle_detail` where `activityId`=? and `time`>? and unionGroup=?";
    public List<PeakSeacraftBattleDetailData> getPeakSeacraftBattleDetailDataList(int activityId, long time, int unionGroup) {
        List<PeakSeacraftBattleDetailData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftBattleDetailDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, time);
            call.setLong(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftBattleDetailData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    public static String updatePeakSeacraftBattleDetailDataSql = "update `t_c_activity_peak_seacraft_battle_detail` set raidAttackMap=?,attackMap=?,useGoodMap=?,time=? where activityId=? and unionGroup=? and unionId=? and masterType=? and targetUnionId=? and round=? and area=? and groupNum=?";
    @Override
    protected boolean updateData(PeakSeacraftBattleDetailData peakSeacraftBattleDetailData) {
        peakSeacraftBattleDetailData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftBattleDetailData.getRaidAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftBattleDetailData.getAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftBattleDetailData.getUseGoodMap())));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftBattleDetailData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftBattleDetailData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getMasterType()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftBattleDetailData.getTargetUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getArea()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getGroupNum()));
        if (getDb().executeNoneQuery(updatePeakSeacraftBattleDetailDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftBattleDetailDataSql = "insert into `t_c_activity_peak_seacraft_battle_detail`(activityId,unionGroup,unionId,masterType,targetUnionId,round,area,groupNum,raidAttackMap,attackMap,useGoodMap,time) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftBattleDetailData peakSeacraftBattleDetailData) {
        peakSeacraftBattleDetailData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftBattleDetailData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getMasterType()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftBattleDetailData.getTargetUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getArea()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftBattleDetailData.getGroupNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftBattleDetailData.getRaidAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftBattleDetailData.getAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftBattleDetailData.getUseGoodMap())));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftBattleDetailData.getTime()));
        if (getDb().executeNoneQuery(insertPeakSeacraftBattleDetailDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftBattleDetailData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftBattleDetailData peakSeacraftBattleDetailData = new PeakSeacraftBattleDetailData();
        peakSeacraftBattleDetailData.setActivityId(rs.getInt("activityId"));
        peakSeacraftBattleDetailData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftBattleDetailData.setUnionId(rs.getString("unionId"));
        peakSeacraftBattleDetailData.setMasterType(rs.getInt("masterType"));
        peakSeacraftBattleDetailData.setTargetUnionId(rs.getString("targetUnionId"));
        peakSeacraftBattleDetailData.setRaidAttackMap(JSON.parseObject(rs.getString("raidAttackMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftBattleDetailInfo>>() {}));
        peakSeacraftBattleDetailData.setAttackMap(JSON.parseObject(rs.getString("attackMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftBattleDetailInfo>>() {}));
        peakSeacraftBattleDetailData.setUseGoodMap(JSON.parseObject(rs.getString("useGoodMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftBattleDetailInfo>>() {}));
        peakSeacraftBattleDetailData.setArea(rs.getInt("area"));
        peakSeacraftBattleDetailData.setGroupNum(rs.getInt("groupNum"));
        peakSeacraftBattleDetailData.setTime(rs.getLong("time"));
        peakSeacraftBattleDetailData.setRound(rs.getInt("round"));

        return peakSeacraftBattleDetailData;
    }
}
